﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    /// <summary>
    /// 重载母版页的OnInit事件
    /// </summary>
    /// <param name="e"></param>
    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserInfo"] == null) //没有登陆
            {
                //将可访问页面锁定为index.aspx或login.aspx
                String temp = Request.FilePath.Substring(Request.FilePath.LastIndexOf("/") + 1);
                temp = temp.ToLower();
                if (temp != "index.aspx" && temp != "login.aspx") 
                {
                    Response.Redirect("login.aspx");//如果当前不是主页的，"/index.aspx" 则跳转到主页
                }
                else
                {
                    //将导航栏设成没登陆状态
                    LogoutState();
                } 
            }
            else //如果已经登陆了，则设置当母版页的状态
            {
                UserInfo user = new UserInfo();
                GetUserFromSession(ref user);
                LoginState(user.Popedom);
            }
        }
        base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string address = System.Configuration.ConfigurationManager.AppSettings["address"];
        logo.Attributes["src"]=address+"/resources/logo.png";
        DivIndex.InnerHtml = "<a href=\"" + address + "/index.aspx\">首页</a>";
        DivInfo.InnerHtml = "<a href=\"" + address + "/Pages/userinfo.aspx\">我的信息</a>";
        DivLogin.InnerHtml = "<a href=\"" + address + "/Pages/login.aspx\">登陆</a>";
        DivManage.InnerHtml = "<a href=\"" + address + "/Pages/manage.aspx\">我要管理</a>";
        DivUpload.InnerHtml = "<a href=\"" + address + "/Pages/upload.aspx\">我要上传</a>";
    }

    /// <summary>
    /// 根据登陆身份设置不同的导航栏内容
    /// </summary>
    /// <param name="identity"></param>
    public void LoginState(int identity)
    {
        DivInfo.Visible = true;
        DivUpload.Visible = true;
        if (identity > 0) { DivManage.Visible = true; }
        else { DivManage.Visible = false; }
        DivLogin.Visible = false;
        BtnLogout.Visible = true;
    }

    /// <summary>
    /// 注销状态
    /// </summary>
    public void LogoutState()
    {
        DivInfo.Visible = false;
        DivUpload.Visible = false;
        DivManage.Visible = false;
        DivLogin.Visible = true;
        BtnLogout.Visible = false;
    }

    protected void BtnLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        LogoutState();
        Response.Redirect(System.Configuration.ConfigurationManager.AppSettings["address"]+"/index.aspx");
    }

    /// <summary>
    ///  首先使用者要给出一个UserInfo的实例作为引用型的参数
    ///  如果Get操作成功则返回true，否则：false
    ///  如果成功，则UserInfo的的内容被修改
    /// </summary>
    /// <param name="userinfo"></param>
    /// <returns></returns>
    public bool GetUserFromSession(ref UserInfo userInfo)
    {
        if (Session["UserInfo"] == null) return false;
        else
        {
            userInfo = (Session["UserInfo"] as UserInfo);
            return true;
        }

    }
    /// <summary>
    ///  把内存中User的实例保存到服务器的session中去
    /// </summary>
    /// <param name="userinfo"></param>
    public void SetUserToSession(UserInfo userInfo)
    {
        Session["UserInfo"] = userInfo;
    }
}
